package com.atues.service.impl;

import com.atues.domain.ResponseResult;
import com.atues.domain.SystemConstants;
import com.atues.domain.entity.Comment;
import com.atues.domain.vo.CommentVo;
import com.atues.domain.vo.PageVo;
import com.atues.emnu.AppHttpCodeEnum;
import com.atues.exception.SystemException;
import com.atues.service.SysUserService;
import com.atues.utils.BeanCopyUtils;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.atues.mapper.CommentMapper;
import com.atues.service.CommentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.ArrayList;
import java.util.List;


/**
 * 评论表(Comment)表服务实现类
 *
 * @author Liqun
 * @since 2024-09-05 16:05:04
 */
@Service
public class CommentServiceImpl extends ServiceImpl<CommentMapper, Comment> implements CommentService {
    @Autowired
    private SysUserService sysUserService;

    @Override
    public ResponseResult commentList(String commentType,Long articleId, Integer pageNum, Integer pageSize) {
        LambdaQueryWrapper<Comment> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(SystemConstants.ARTICLE_COMMENT.equals(commentType),
                Comment::getArticleId, articleId)
                .eq(Comment::getRootId, SystemConstants.COMMENT_ROOT_VALUE)
                .orderByAsc(Comment::getCreateTime);

        //评论类型
        wrapper.eq(Comment::getType, commentType);

        Page<Comment> page = new Page<>(pageNum, pageSize);
        page(page, wrapper);
        List<CommentVo> commentVos = this.toCommentVoList(page.getRecords());
        //处理子集评论
        commentVos.forEach(commentVo ->

           commentVo.setChildren(getChildren(commentVo.getId()))
        );
        return ResponseResult.okResult(new PageVo(commentVos,page.getTotal()));
    }

    @Override
    public ResponseResult addComment(Comment comment) {
        if (!StringUtils.hasText(comment.getContent())) {
            throw new SystemException(AppHttpCodeEnum.CONTENTNOTNULL);
        }
        save(comment);
        return null;
    }

    private List<CommentVo> getChildren(Long id) {
        LambdaQueryWrapper<Comment> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Comment::getRootId, id)
                .orderByAsc(Comment::getCreateTime);
        List<Comment> comments = list(wrapper);
        return toCommentVoList(comments);
    }

    private List<CommentVo> toCommentVoList(List<Comment> comments){
        List<CommentVo> commentVos = BeanCopyUtils.copyBeanList(comments, CommentVo.class);
        commentVos.forEach(commentVo -> {
            String nickName = sysUserService.getById(commentVo.getCreateBy()).getNickName();
            commentVo.setUserName(nickName);
            Long toCommentUserId = commentVo.getToCommentUserId();
            commentVo.setToCommentUserName(
                    toCommentUserId == -1 ? "-1" :
                            sysUserService.getById(toCommentUserId).getNickName()
            );
        });
        return commentVos;
    }

}

